package com.ruoyi.web.controller.common;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/token/refresh")
public class TokenController {
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    //todo 验证token的有效期 应该是 前段将储存的token传入 后端从redis中拿取token判断有效期 如果有效期没过说明token有效
    @GetMapping
    public AjaxResult authToken(String token) {
        if (token == null || StringUtils.isEmpty(token)) {
            return AjaxResult.error("token不能为空");
        }
        //从redis中获取token,如果redis中没有token则说明过期
        Object redisToken = redisTemplate.opsForValue().get("tokens" + token);
        if (redisToken == null) {
            return AjaxResult.error("token已过期");
        } else {
            return AjaxResult.success("token有效");

        }
    }
}
